package com.oocl.o2o.client.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.o2o.client.entity.Commentary;
import com.oocl.o2o.client.entity.Customer;
import com.oocl.o2o.client.service.CommentaryService;
import com.oocl.o2o.client.vo.CommentaryVO;
import com.oocl.o2o.client.vo.EvaluateVO;


@Controller
public class CommentaryCotroller {
	
	@Resource(name="commentaryServiceImpl")
	private CommentaryService commentaryService;
	
	@ResponseBody
	@GetMapping("/commentary/customerId/{customerId}")
	public List<Commentary> findCommentaryByCustomerId(@PathVariable("customerId") String customerId){
		List<Commentary> commentaries = new ArrayList<Commentary>();
		commentaries = commentaryService.findCommentaryByCustomerId(customerId);
		return commentaries;
	}
	

	@GetMapping("/commentary/merchantName/{merchantName}")
	public String findCommentaryByMerchantId(@PathVariable("merchantName") String merchantName,HttpServletRequest request){
		List<CommentaryVO> commentaryVOs = new ArrayList<CommentaryVO>();
		commentaryVOs = commentaryService.findCommentaryByMerchantName(merchantName);
		request.setAttribute("commentaryVOs", commentaryVOs);
		return "commentary";
	}
	
	
	
	@ResponseBody
	@GetMapping("/commentary/id/{id}")
	public Commentary findById(@PathVariable("id") String id){
		Commentary commentary = new Commentary();
		System.out.println(id);
		commentary = commentaryService.findById(id);
		return commentary;
	}
	
	@ResponseBody
	@PostMapping("/commentary")
	public Commentary add(@RequestBody EvaluateVO evaluateVO, HttpSession session){
		Commentary c = new Commentary();
		Customer customer = (Customer) session.getAttribute("customer");
		c = commentaryService.add(evaluateVO,customer);
		return c;
		
	}
	
	@ResponseBody
	@PutMapping("/commentary")
	public Commentary update(@RequestBody Commentary commentary){
		System.out.println(commentary.getId());
		Commentary c = new Commentary();
		c = commentaryService.update(commentary);
		return c;
		
	}
	
	
	
	
	
}
